package fi.helsinki.cs.ohtu10k.schedule;

import java.util.ArrayList;
import java.util.List;
import java.io.Serializable;
import org.joda.time.DateTime;

/**
 * Class representing courses with attached events.
 * @author Jani Rönkä
 * @author Thomas Nyman
 *
 */
class PeriodCourse implements ScheduleCourse, Serializable {
	
	/**
	 * Serial version ID used for serialization.  
	 */
	private static final long serialVersionUID = -5413870724318278380L;
	
	private DateTime startDate;
	private DateTime endDate;
	private String name;
	private String abbr;
	private List<ScheduleEvent> seList;
	
	/**
	 * Hidden parameterless constructor.
	 */
	@SuppressWarnings("unused")
	private PeriodCourse() {}
	
	PeriodCourse(DateTime startDate, DateTime endDate, String name, String abbr) 
		throws IllegalArgumentException {
		
		if( startDate == null || endDate == null || name == null || abbr == null )
			throw new IllegalArgumentException();
		
		this.startDate = startDate;
		this.endDate = endDate;
		this.name = name;
		this.abbr = abbr;
		seList = new ArrayList<ScheduleEvent>();
	}
	
	/**
	 * Returns start date of course.
	 * @return Start date of course as Date.
	 */
	@Override
	public DateTime getStartDate() {
		return this.startDate;
	}

	/**
	 * Returns end date of course.
	 * @return End date of course as Date.
	 */
	@Override
	public DateTime getEndDate() {
		return this.endDate;
	}
	
	/**
	 * Returns name of course.
	 * @return Name of course as String.
	 */
	@Override
	public String getName() {
		return this.name;
	}
	
	/**
	 * Returns course abbreviation.
	 * @return Course Abbreviation as String.
	 */
	@Override
	public String getAbbreviation() {
		return this.abbr;
	}
	
	/**
	 * Returns all events associated to this course.
	 * @return Array of all events associated to course.
	 */
	@Override
	public ScheduleEvent[] getEvents() {
		return seList.toArray( new ScheduleEvent[ 0 ] );
	}
	
	/**
	 * Adds an event to a list of events associated with this course.
	 */
	@Override
	public void addEvent(ScheduleEvent event) {
		seList.add( event );
	}
	
	/**
	 * Removes specified event from a list of events associated with this course.
	 */
	@Override
	public void delEvent(ScheduleEvent event) {
		for( int i = 0; i < seList.size(); ++i ) {
			if( seList.get( i ).equals( event ) ) {
				seList.remove( i );
				break;
			}
		}
	}
	
	/**
	 * Returns String representation of course.
	 * @return String representation of course. 
	 */
	@Override
	public String toString() {
		return this.getName();
	}

}
